<?php

namespace app\index\controller;

use app\model\Member;
use app\model\Order;
use think\exception\ValidateException;
use app\model\Config;
use app\model\Points;
use app\model\PointsStaging;

class AutoController extends Base
{

    public function index()
    {
//        \think\facade\Log::write("22222222222");

        // 积分返利
//        PointsStaging::uppoints();
        $this->uppoints();
        return $this->json(['data' => time_format(time(), 'Y-m-d H:i:s'), 'msg'=>'积分返利脚本执行成功']);
    }

    public  function uppoints()
    {
        $weid = weid();
        $pointsConfig =  Config::getconfig('points');
        if (empty($pointsConfig['stagingweek'])) {
            $pointsConfig['stagingweek'] = 1;
        }

        // && date('w') == $pointsConfig['stagingweek']
        if ($pointsConfig['is_staging'] == 1 && $pointsConfig['stagingpoints'] > 0 ) {
            $orderItem = Order::where([])
                ->field('return_points,pay_points,id,uid,order_num_alias')
                ->where('return_points', '>', 0)
                ->order('id', 'desc')
//                ->where('id', 68)
//                ->where('uid', 8)
                ->select()->toArray();

            $PointsModel = new Points();
            $member = new Member();
            $time = time();
            if ($orderItem) {
                foreach ($orderItem as $vo) {
                    $points = 0;
                    $count = $PointsModel->where([
                        'order_id' => $vo['id'],
                        'is_tasks' => 1,
                        'from_type' => 1
                    ])->count();
                    if($count == 0){
                        $points = $vo['return_points'] * 0.005;
                        $description = '订单'.$vo['order_num_alias'].'第1返利'.$points;
                        $points = round($points, 2);
                    }else{

                        $pointsItem = $PointsModel->where([
                            'order_id' => $vo['id'],
                            'is_tasks' => 1,
                            'from_type' => 1
                        ])->order('id desc')->find();
                        $pointsPoints = $PointsModel->where([
                            'order_id' => $vo['id'],
                            'is_tasks' => 1,
                            'from_type' => 1
                        ])->sum('points');

                        $points = $pointsItem['received_points'] * $pointsConfig['increment'];

                        if(($pointsPoints + $points) > $vo['return_points'] ){
                            $points = $vo['return_points'] - $pointsPoints;
                        }elseif ($pointsPoints == $vo['return_points']){
                            continue;
                        }
                        $points = round($points, 2);
                        $description = '订单' . $vo['order_num_alias'] . '第' . ($count + 1) . '返利' . $points;
                    }

                    if ($points) {
                        $insert = [
                            'weid' => $weid,
                            'uid' => $vo['uid'],
                            'order_id' => $vo['id'],
                            'order_num_alias' => $vo['order_num_alias'],
                            'points' => $points,
                            'staging_day' => date("Y-m-d"),
                            'description' => $description,
                            'received_points' => $points,
                            'prefix' => 1,
                            'from_type' => 1,
                            'is_tasks' => 1,
                            'create_time' => $time
                        ];
                        $insert2 = [
                            'weid' => $weid,
                            'uid' => $vo['uid'],
                            'order_id' => $vo['id'],
                            'order_num_alias' => $vo['order_num_alias'],
                            'points' => $points,
                            'received_points' => $points,
                            'staging_day' => date("Y-m-d"),
                            'description' => $description,
                            'prefix' => 2,
                            'from_type' => 2,
                            'is_tasks' => 1,
                            'create_time' => $time
                        ];

                        $PointsModel->insert($insert);
                        $PointsModel->insert($insert2);
                        $member->where('id', $vo['uid'])
                            ->inc('points', $points)
                            ->update();
//                        echo $member->getLastSql() . "\n";
                        $member->where('id', $vo['uid'])
                            ->dec('notreceived_points', $points)
                            ->update();
//                        echo $member->getLastSql() . "\n";

                    }

                }
            }
        }
    }
}
